package com.base.pig.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.base.pig.dto.AuditAddDto;
import com.base.pig.dto.AuditDto;
import com.base.pig.entity.AlarmSetting;
import com.base.pig.entity.Audit;
import com.base.pig.entity.Auditline;
import com.base.pig.entity.Auditpoint;
import com.base.pig.service.AuditService;
import com.base.pig.service.AuditlineService;
import com.base.pig.service.AuditpointService;
import com.base.pig.util.R;
import com.base.pig.vo.AuditPageVo;
import com.base.pig.vo.AuditVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  管线审批
 * </p>
 *
 * @author yjh
 * @since 2023-06-20
 */
@RestController
@Api(value = "AuditController", tags = "管线审批")
@RequestMapping("/audit")
public class AuditController {

    @Autowired
    public AuditService auditService;

    @Autowired
    public AuditlineService auditlineService;

    @Autowired
    public AuditpointService auditpointService;

 /*   @ApiOperation(value = "条件查询")
    @PostMapping("/list")
    public R list(@RequestBody Audit audit){
        List<Audit> auditList = auditService.list(new QueryWrapper<>(audit));
        return R.ok(auditList,"查询成功");
    }*/

    @ApiOperation(value = "分页查询")
    @PostMapping("/pageList/{pageNum}/{pageSize}")
    public R pageList(@PathVariable("pageNum")Long pageNum,
                  @PathVariable("pageSize")Long pageSize,
                  @RequestBody Audit audit){
        AuditPageVo auditPageVo = new AuditPageVo();
        IPage<Audit> page = auditService.page(
        new Page<>(pageNum, pageSize),
        new QueryWrapper<>(audit).orderByDesc("applytime"));
        auditPageVo.setPage(page);
        JSONArray arr = new JSONArray();
        page.getRecords().forEach( a ->{
            if(a.getPipetype().equals("管点")){
                Auditpoint point = auditpointService.getById(a.getRid());
                arr.add(point.getXzb()+","+point.getYzb());
            }else{
                Auditline line = auditlineService.getById(a.getRid());
                arr.add(line.getSnodid()+","+line.getEnodid());
            }
        });
        auditPageVo.setLocations(arr);
        return R.ok(auditPageVo,"分页查询成功");
    }

    @ApiOperation(value = "根据id查询详情")
    @GetMapping("/get/{id}")
    public R get(@PathVariable("id") Long id){
        AuditVo vo = new AuditVo();
        Audit audit = auditService.getById(id);
        vo.setAudit(audit);
        if(audit.getPipetype().equals("管点")){
            //Auditpoint point = auditpointService.getOne(Wrappers.<Auditpoint>query().lambda().eq(Auditpoint::getUsId,audit.getRid()));
            Auditpoint point = auditpointService.getById(audit.getRid());
            vo.setPoint(point);
        }else{
            //Auditline line = auditlineService.getOne(Wrappers.<Auditline>query().lambda().eq(Auditline::getUsId,audit.getRid()));
            Auditline line = auditlineService.getById(audit.getRid());
            vo.setLine(line);
        }
        return R.ok(vo,"查询成功");
    }

    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public R save(@RequestBody AuditAddDto dto){
        Audit audit = dto.getAudit();
        Auditline line = dto.getLine();
        Auditpoint point = dto.getPoint();
        String rid = "";
        audit.setApplytime(new Date());
        audit.setAudit("未审核");
        if(audit.getPipetype().equals("管点")){
            long count = auditpointService.count(Wrappers.<Auditpoint>query().lambda()
                    .eq(Auditpoint::getAudittype,"未审核")
                    .eq(Auditpoint::getUsId,point.getUsId()));
            if(count != 0){
                return R.failed(null,"该管点正在审批中");
            }
            point.setAudittype("未审核");
            auditpointService.save(point);
            Auditpoint p = auditpointService.getOne(Wrappers.<Auditpoint>query().lambda()
                    .eq(Auditpoint::getAudittype,"未审核")
                    .eq(Auditpoint::getUsId,point.getUsId()));
            rid = p.getId()+"";
        }else{
            long count = auditlineService.count(Wrappers.<Auditline>query().lambda()
                    .eq(Auditline::getAudittype,"未审核")
                    .eq(Auditline::getUsId,line.getUsId()));
            if(count != 0){
                return R.failed(null,"该管线正在审批中");
            }
            line.setAudittype("未审核");
            auditlineService.save(line);
            Auditline p = auditlineService.getOne(Wrappers.<Auditline>query().lambda()
                    .eq(Auditline::getAudittype,"未审核")
                    .eq(Auditline::getUsId,line.getUsId()));
            rid = p.getId()+"";
        }
        audit.setRid(rid);
        boolean b = auditService.save(audit);
        if (b){
        return R.ok(b,"添加成功");
        }
        return R.failed(null,"添加失败");
    }

    @ApiOperation(value = "审核")
    @PostMapping("/audit")
    public R audit(@RequestBody AuditDto auditDto){
        Audit audit = auditService.getById(auditDto.getId());
        audit.setAudittime(new Date());
        audit.setAudit(auditDto.getAudit());
        audit.setAuditreason(auditDto.getAuditreason());
        boolean b = auditService.updateById(audit);
        if(audit.getPipetype().equals("管点")){
            Auditpoint point  = auditpointService.getById(audit.getRid());
            point.setAudittype("已审核");
            auditpointService.updateById(point);
        }else{
            Auditline line  = auditlineService.getById(audit.getRid());
            line.setAudittype("已审核");
            auditlineService.updateById(line);
        }
        if (b){
            return R.ok(b,"审核成功");
        }
        return R.failed(null,"审核失败");
    }

 /*   @ApiOperation(value = "根据id删除")
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id){
        auditService.removeById(id);
        return R.ok(null,"删除成功");
    }*/

  /*  @ApiOperation(value = "根据id修改")
    @PostMapping("/update/{id}")
    public R update(@RequestBody Audit audit){
        if(audit.getId()!=null)
        {
            long count = auditService.count(Wrappers.<Audit>query().lambda().eq(Audit::getId,audit.getId()));
            if(count != 0) {
                 return R.ok( auditService.updateById(audit),"修改成功");
            }
            return R.failed(null,"未找到该数据");
        }
        return R.failed(null,"id不能为空");
    }
*/

}
